<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告标题" prop="adTitle">
        <el-input
          v-model="queryParams.adTitle"
          placeholder="请输入广告标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户端" prop="client">
        <el-input
          v-model="queryParams.client"
          placeholder="请输入客户端"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推送时间" prop="pushTime">
        <el-date-picker clearable
                        v-model="queryParams.pushTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择推送时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="失效日期" prop="expirationDate">
        <el-date-picker clearable
                        v-model="queryParams.expirationDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择失效日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="跳转链接" prop="jumpLink">
        <el-input
          v-model="queryParams.jumpLink"
          placeholder="请输入跳转链接"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链接标题" prop="linkTitle">
        <el-input
          v-model="queryParams.linkTitle"
          placeholder="请输入链接标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['parcel:EAdvertisements:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['parcel:EAdvertisements:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['parcel:EAdvertisements:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['parcel:EAdvertisements:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="EAdvertisementsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column @click="handleUpdate(scope.row)" label="ID" align="center" prop="id"/>
      <el-table-column label="广告标题" align="center" prop="adTitle"/>
      <el-table-column label="客户端" align="center" prop="client"/>
      <!--      <el-table-column label="中文图片" align="center" prop="chineseImage" width="100">-->
      <!--        <template slot-scope="scope">-->
      <!--          <image-preview :src="scope.row.chineseImage" :width="50" :height="50"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        label="中文图片"
        width="180">
        <template #default="scope">
          <img :src="scope.row.chineseImage" alt="" style="width: 100px;height: 100px">
        </template>
      </el-table-column>
      <!--      <el-table-column label="英文图片" align="center" prop="englishImage" width="100">-->
      <!--        <template slot-scope="scope">-->
      <!--          <image-preview :src="scope.row.englishImage" :width="50" :height="50"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        label="图片"
        width="180">
        <template #default="scope">
          <img :src="scope.row.englishImage" alt="" style="width: 100px;height: 100px">
        </template>
      </el-table-column>
      <el-table-column label="推送时间" align="center" prop="pushTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pushTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失效日期" align="center" prop="expirationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status == 1 ? '已启用' : scope.row.status == 2 ? '已下架' : '已过期' }}
        </template>
      </el-table-column>
      <el-table-column label="跳转链接" align="center" prop="jumpLink"/>
      <el-table-column label="链接标题" align="center" prop="linkTitle"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['parcel:EAdvertisements:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.status==1"
            @click="xiajia(scope.row.id)"
          >下架
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['parcel:EAdvertisements:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改parcel对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="广告标题" prop="adTitle">
          <el-input v-model="form.adTitle" placeholder="请输入广告标题"/>
        </el-form-item>
        <el-form-item label="客户端" prop="client">
          <el-input v-model="form.client" placeholder="请输入客户端"/>
        </el-form-item>
        <el-form-item label="中文图片" prop="chineseImage">
          <image-upload v-model="form.chineseImage"/>
        </el-form-item>
        <el-form-item label="英文图片" prop="englishImage">
          <image-upload v-model="form.englishImage"/>
        </el-form-item>
        <el-form-item label="推送时间" prop="pushTime">
          <el-date-picker clearable
                          v-model="form.pushTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择推送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失效日期" prop="expirationDate">
          <el-date-picker clearable
                          v-model="form.expirationDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择失效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="跳转链接" prop="jumpLink">
          <el-input v-model="form.jumpLink" placeholder="请输入跳转链接"/>
        </el-form-item>
        <el-form-item label="链接标题" prop="linkTitle">
          <el-input v-model="form.linkTitle" placeholder="请输入链接标题"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button v-if="title=='修改parcel'&&form.status==1" type="primary" @click="xiajia(form.id)">下 架</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEAdvertisements,
  getEAdvertisements,
  delEAdvertisements,
  addEAdvertisements,
  updateEAdvertisements,
  updateEAdvertisementsStatus
} from "@/api/parcel/EAdvertisements";

export default {
  name: "EAdvertisements",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // parcel表格数据
      EAdvertisementsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        adTitle: null,
        client: null,
        chineseImage: null,
        englishImage: null,
        pushTime: null,
        expirationDate: null,
        status: null,
        jumpLink: null,
        linkTitle: null
      },
      newTime: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        adTitle: [
          {required: true, message: "广告标题，展示广告的主要内容不能为空", trigger: "blur"}
        ],
        client: [
          {required: true, message: "客户端，表明该广告适用于哪种客户端平台不能为空", trigger: "blur"}
        ],
        chineseImage: [
          {required: true, message: "中文图片，与广告相关的中文图片资源信息不能为空", trigger: "blur"}
        ],
        englishImage: [
          {required: true, message: "英文图片，与广告相关的英文图片资源信息不能为空", trigger: "blur"}
        ],
        pushTime: [
          {required: true, message: "推送时间，广告计划开始推送的具体时间不能为空", trigger: "blur"}
        ],
        expirationDate: [
          {required: true, message: "失效日期，广告失去效力的时间节点不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态，体现广告当前所处的状态情况不能为空", trigger: "change"}
        ],
        jumpLink: [
          {required: true, message: "跳转链接，点击广告后将要跳转去的网页链接不能为空", trigger: "blur"}
        ],
        linkTitle: [
          {required: true, message: "链接标题，跳转链接所对应的展示标题不能为空", trigger: "blur"}
        ]
      }
    };
  },
  watch: {
    'form.pushTime': function (newValue, oldValue) {
      const currentDate = new Date();
      const currentDateOnly = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDay());
      if (newValue) {
        const inputDate = new Date(newValue);
        const inputDateOnly = new Date(inputDate.getFullYear(), inputDate.getMonth(), inputDate.getDay());
        if (inputDateOnly < currentDateOnly) {
          // 弹出提示框告知用户推送时间不能小于当前时间
          this.$message.error('推送时间不能小于当前时间');
          console.log(1111111)
          // 可以根据需求选择是否将推送时间重置为当前时间，示例如下：
          this.form.pushTime = currentDateOnly;
        }
      }
    },
    'form.expirationDate': function (newValue, oldValue) {
      if (this.form.pushTime && newValue && newValue < this.form.pushTime) {
        // 可以选择弹出提示框
        this.$message.error('失效日期不能小于推送时间');
        // 或者直接将失效日期设置为推送时间
        this.form.expirationDate = this.form.pushTime;
      }
    }
  },
  created() {
    this.getList();

  },
  methods: {
    /** 查询parcel列表 */
    getList() {
      this.loading = true;
      listEAdvertisements(this.queryParams).then(response => {
        this.EAdvertisementsList = response.rows;
        this.total = response.total;
        this.loading = false;
        let newTime = new Date().getTime();
        console.log("当前日期")
        console.log(newTime)
        this.EAdvertisementsList.forEach(item => {
          const expirationDateObj = Date.parse(item.expirationDate);
          if (newTime >= expirationDateObj) {
            console.log("当前失效日期")
            console.log(expirationDateObj)
            item.status = 3;
          }
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        adTitle: null,
        client: null,
        chineseImage: null,
        englishImage: null,
        pushTime: null,
        expirationDate: null,
        createTime: null,
        status: null,
        jumpLink: null,
        linkTitle: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加parcel";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEAdvertisements(id).then(response => {
        this.form = response.data;
        this.open = true;
        console.log(11111111111)
        this.title = "修改parcel";
      });
    },
    xiajia(id) {
      updateEAdvertisementsStatus(id).then(row => {
        this.$modal.msgSuccess("下架成功");
        this.getList();
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEAdvertisements(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEAdvertisements(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除parcel编号为"' + ids + '"的数据项？').then(function () {
        return delEAdvertisements(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('parcel/EAdvertisements/export', {
        ...this.queryParams
      }, `EAdvertisements_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
